import { passApplyApi, refuseApplyApi } from "@/api/waybillManagement/insurance/insuranceApproval";

import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formState, emit, formDataRef, passData, loading } = state;
  const { resetFields } = FormHook({ formState });

  const onFinish = () => {
    // const { customerServiceRemark } = formState;
    const params = {
      idList: Array.isArray(passData.value) ? passData.value : [passData.value.id],
      insuranceFeeRatio: formState.insuranceFeeRatio,
      insuranceAmount: Array.isArray(passData.value) ? undefined : Number(((passData.value.insuranceAmount * (formState.insuranceFeeRatio * 100)) / 100 / 100).toFixed(4))
    };
    loading.value = true;
    return passApplyApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };

  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };

  const refuseApplyHandle = () => {
    loading.value = true;
    return refuseApplyApi([passData.value.id])
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  return {
    onFinish,
    onFinishFailed,
    resetHandle,
    resetFields,
    refuseApplyHandle
  };
}
